/**
 * 
 */
	var oTable;
	$(function() {
		oTable = $('#table_report').dataTable( {
			"sDom" : '<"wrapper"lrtf<"row-fluid"<"span6"i><"span6"p>>>',
	        //"bRetrieve" : true,
	        //"bStateSave": true,
	        "bPaginate" : true, 
	        "fnServerData": retrieveData,
	        
	        "bAutowidth" : true,
            "iDisplayLength" : 10,  
            "bScrollCollapse" : false,  
            //"bInfo" : true,   
            "bSort" : false,   
            "bFilter" : false,   
            "bLengthChange": false,
            "oLanguage": DATATABLE_CN,
            
	        "bDestroy" : true, 
	        "sServerMethod": "POST",
	        "fnInitComplete": function(oSettings, json) {
	        	setHeight()
	        },
			
			"bProcessing": false,
	        "bServerSide": true,
	        "aoColumns": [
	            { "mData" : null ,
	              "mRender": function(data, type, object){
			    	return "<label><input id='"+object["id"]+"' type='checkbox'/><span class='lbl'></span></label>";
			     },
			     "sClass": "center"
			     },
				{ "mData": "loginname",  "sClass": "center" },
	            { "mData": "pwd" ,  "sClass": "center" },
	            { "mData": "optcode" ,  "sClass": "center" , "mRender": function(data, type, object){
	            	return optMap[data]?optMap[data]:data;
	            } },
	            { "mData": "status" ,  "sClass": "center" , "mRender": function(data, type, object){
	            	return data?data=='0'?"空闲":"使用中["+(object.curuser&&object.curuser!=""?object.curuser:"无用户信息")+"]":data;
	            } },
	            { "mData": "actived" ,  "sClass": "center", "mRender": function(data, type, object){
	            	return data?data=='0'?"停用":"启用":data;
	            } },
	            { "mData": null ,
	              "mRender": operations,
	              "sClass": "center"}
	        ],
	        "sAjaxSource": "account/accounts"
			});
		
		$('table th input:checkbox').on('click' , function(){
			var that = this;
			$(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function(){
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});
				
		});

		$('[data-rel=tooltip]').tooltip();
		
		getOpts("optcode", true);
		
		function operations(data, type, object){
			return "<div class='hidden-phone visible-desktop btn-group'>"+
				"<button title='Edit' onclick='editAccount(\""+object["id"]+"\")' class='btn btn-mini btn-info'><i class='icon-edit'></i></button>"+
				"<button title='Consumer Details' onclick='viewDetail(\""+object["id"]+"\")' class='btn btn-mini btn-success'><i class='icon-list'></i></button>"+
				"<button title='Delete' onclick='delAccount(\""+object["id"]+"\")' class='btn btn-mini btn-danger'><i class='icon-trash'></i></button>"+
				"</div>";
		}
		
		function retrieveData(sSource, aoData, fnCallback){
			aoData = $.demonPlugin.collectFormData(aoData);
			$.demonPlugin.asyncData("POST" , sSource, aoData, fnCallback);
		}
		
		$("#iSearch").click(function(){
			oTable.fnDraw();
		});
		
		$("#delAccount").click(function(){
			var list = [];
			$("#table_report").find('tr > td:first-child input:checked')
			.each(function(){
				list.push($(this).attr("id"));
			});
			if (list.length==0){
				alert("请选择需要删除的数据");
				return;
			}
			delAccount(list);
		});
		
		$("#addAccount").click(function(){
			window.location.href = "account/add";
		});
	});
	
	function delAccount(id){
		if ( !confirm("确定删除?") )
			return false;
		if ( ! id instanceof Array )
			id = [id];
		$.demonPlugin.asyncData("DELETE", "account/delete", $.param({"ids": id}, true),
				function (data){
					if (data.status == "ok"){
						alert("删除成功!")
						oTable.fnDraw();
					}else{
						alert("删除失败，请重试");
					}
				}, function(data){
					alert("删除失败，请重试");
				});
	}
	
	function editAccount(id){
		window.location.href = "account/edit?id="+id;
	};
	
	function viewDetail(id){
		console.log("TODO VIEW DETAILS");
	}
	
	function viewDetail(id){
		window.location.href = "userdetail/account/"+id;
	}